package com.zzb.myshowme.utils;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * Created by Administrator on 2016/1/1.
 * /data/data/<application package name>/shared_prefs/ 保存在这个目录下
 */
public class SharedPreferencesUtil {
    private static final String PREFERENCES_PRIVATE = "com.zzb.preferences.private";
    public static String LOGIN = "myshowme.login";    //用户登录业务账号

    public static void setBoolean(Context context, String key, boolean value) {
        SharedPreferences preferences = context.getSharedPreferences(PREFERENCES_PRIVATE, context.MODE_PRIVATE);//第一个参数是存储时的名称（文件名），第二个参数则是文件的打开方式
        SharedPreferences.Editor editor = preferences.edit();
        editor.putBoolean(key, value).commit();
    }

    public static boolean getBoolean(Context context, String key) {
        SharedPreferences preferences = context.getSharedPreferences(PREFERENCES_PRIVATE, context.MODE_PRIVATE);
        return preferences.getBoolean(key, false);
    }

    public static boolean getBoolean(Context context, String key, boolean defaultValue) {
        SharedPreferences preferences = context.getSharedPreferences(PREFERENCES_PRIVATE, context.MODE_PRIVATE);
        return preferences.getBoolean(key, defaultValue);
    }


    public static void setString(Context context, String key, String value) {
        SharedPreferences preferences = context.getSharedPreferences(PREFERENCES_PRIVATE, context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString(key, value).commit();


    }

    public static String getString(Context context, String key, String defaultValue) {
        SharedPreferences preferences = context.getSharedPreferences(PREFERENCES_PRIVATE, context.MODE_PRIVATE);
        return preferences.getString(key, defaultValue);

    }

    public static void setInt(Context context, String key, int value) {
        SharedPreferences preferences = context.getSharedPreferences(PREFERENCES_PRIVATE, context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putInt(key, value).commit();


    }

    public static int getInt(Context context, String key, int defaultValue) {
        SharedPreferences preferences = context.getSharedPreferences(PREFERENCES_PRIVATE, context.MODE_PRIVATE);
        return preferences.getInt(key, defaultValue);
    }

    /**
     * 获取保存在应用SharedPreferences中的私有数据
     *
     * @param context      上下文
     * @param key          保存键
     * @param defaultValue 默认值
     * @return 获取的数据
     */
    public static String getPrivatePreferences(Context context, String key, String defaultValue) {
        SharedPreferences preferences = context.getSharedPreferences(PREFERENCES_PRIVATE, Context.MODE_PRIVATE);
        return preferences.getString(key, defaultValue);
    }

    /**
     * 保存私有数据到应用SharedPreferences
     *
     * @param context 上下文
     * @param key     保存键
     * @param value   保存值
     * @return true, 保存成功; false, 保存失败
     */
    public static boolean setPrivatePreferences(Context context, String key, String value) {
        SharedPreferences preferences = context.getSharedPreferences(PREFERENCES_PRIVATE, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        if (value == null) {
            value = "";
        } else {
            value = value.trim();
        }
        editor.putString(key, value);
        return editor.commit();
    }

}
